home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 08 - 1992 / 08.06 Oct 92 / Getting Started / IconMania in C / IconMania.c next >
Encoding:
C/C++ Source or Header  |  1992-08-25  |  2.6 KB  |  147 lines  |  [TEXT/KAHL]

  1. /********************************************/
  2. /*                                            */
  3. /*  ResPlotter Code from Column Six of        */
  4. /*                                            */
  5. /*    *** MacTutor, Getting Started ***        */
  6. /*                                          */
  7. /*    Copyright 1992, Dave Mark                   */
  8. /*                                            */
  9. /********************************************/
  10.  
  11. #define    kBaseResID            128
  12. #define    kMoveToFront        (WindowPtr)-1
  13. #define kRandomUpperLimit    32768
  14.  
  15.  
  16. /***************/
  17. /*  Functions  */
  18. /***************/
  19.  
  20. void    ToolBoxInit( void );
  21. void    WindowInit( void );
  22. void    MainLoop( void );
  23. void    DrawRandomIcon( CIconHandle theIcon );
  24. void    RandomPoint( Point    *pointPtr );
  25. short    Randomize( short range );
  26.  
  27.  
  28. /****************** main ***************************/
  29.  
  30. void    main( void )
  31. {
  32.     ToolBoxInit();
  33.     WindowInit();
  34.     MainLoop();
  35. }
  36.  
  37.  
  38. /****************** ToolBoxInit *********************/
  39.  
  40. void    ToolBoxInit( void )
  41. {
  42.     InitGraf( &thePort );
  43.     InitFonts();
  44.     InitWindows();
  45.     InitMenus();
  46.     TEInit();
  47.     InitDialogs( nil );
  48.     InitCursor();
  49. }
  50.  
  51.  
  52. /****************** WindowInit ***********************/
  53.  
  54. void    WindowInit( void )
  55. {
  56.     WindowPtr        window;
  57.     StringHandle    windowTitleH;
  58.  
  59.     window = GetNewWindow( kBaseResID , nil,
  60.                                     kMoveToFront );
  61.     
  62.     if ( window == nil )
  63.     {
  64.         SysBeep( 10 );    /*  Couldn't load the WIND resource!!!  */
  65.         ExitToShell();
  66.     }
  67.     
  68.     windowTitleH = GetString( kBaseResID );
  69.     
  70.     if ( windowTitleH == nil )
  71.     {
  72.         SysBeep( 10 );    /*  Couldn't load the STR resource!!!  */
  73.         ExitToShell();
  74.     }
  75.     
  76.     HLock( (Handle)windowTitleH );
  77.     SetWTitle( window, *windowTitleH );
  78.     HUnlock( (Handle)windowTitleH );
  79.     
  80.     ShowWindow( window );
  81.     SetPort( window );
  82. }
  83.  
  84.  
  85. /****************** MainLoop ***********************/
  86.  
  87. void    MainLoop( void )
  88. {
  89.     CIconHandle    theIcon;
  90.     
  91.     GetDateTime( (unsigned long *)(&randSeed) );
  92.     
  93.     theIcon = GetCIcon( kBaseResID );
  94.     
  95.     if ( theIcon == nil )
  96.     {
  97.         SysBeep( 10 );    /*  Couldn't load the cicn resource!!!  */
  98.         ExitToShell();
  99.     }
  100.     
  101.     while ( ! Button() )
  102.         DrawRandomIcon( theIcon );
  103. }
  104.  
  105.  
  106. /****************** DrawRandomIcon *****************/
  107.  
  108. void    DrawRandomIcon( CIconHandle theIcon )
  109. {
  110.     Point    p;
  111.     Rect    iconRect;
  112.     
  113.     RandomPoint( &p );
  114.     
  115.     SetRect( &iconRect, p.h, p.v, p.h+32, p.v+32 );
  116.     PlotCIcon( &iconRect, theIcon );
  117. }
  118.  
  119.  
  120. /****************** RandomPoint *********************/
  121.  
  122. void    RandomPoint( Point    *pointPtr )
  123. {
  124.     WindowPtr    window;
  125.  
  126.     window = FrontWindow();
  127.     
  128.     pointPtr->h = Randomize( window->portRect.right
  129.         - window->portRect.left );
  130.     pointPtr->v = Randomize( window->portRect.bottom
  131.         - window->portRect.top );
  132. }
  133.  
  134.  
  135. /****************** Randomize **********************/
  136.  
  137. short    Randomize( short range )
  138. {
  139.     long    randomNumber;
  140.     
  141.     randomNumber = Random();
  142.     
  143.     if ( randomNumber < 0 )
  144.         randomNumber *= -1;
  145.     
  146.     return( (randomNumber * range) / kRandomUpperLimit );
  147. }